import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import java.awt.*;
import java.awt.event.*;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;

import java.sql.*;
import javax.swing.*;
import java.awt.CardLayout;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class OwnerEdit extends JScrollPane {

	// Restaurant Info
	private JTextField jtfRestaurantName = new JTextField(10);
	private JTextField jtfRestaurantPhone = new JTextField(10);
	private JTextField jtfRestaurantType = new JTextField(10);
	private JTextField jtfRestaurantAddress = new JTextField(10);
	private JTextField jtfRestaurantDescription = new JTextField(10);

	private JButton jbtSave = new JButton("Save");

	// Food Info
	private JTextField jtfDishName = new JTextField(10);
	private JTextField jtfDishDescription = new JTextField(10);
	private JTextField jtfDishMainIngredient = new JTextField(10);

	private JButton jbtAddDish = new JButton("Add");


	protected void initDB() {
		

		String changeRestaurantStatement = "INSERT INTO restaurant (name, tel, type, address, aboutd) VALUES (restaurantNameEdit, restaurantPhoneEdit, restaurantTypeSet, restaurantAddressEdit, restaurantDescriptionEdit)";

		try {
			Class.forName("com.mysql.jdbc.Driver");

			System.out.println("Driver loaded");

			Connection connection = DriverManager.getConnection(
					"jdbc:mysql://db.student.chalmers.se/spilkov", "spilkov", "pfG87H26");
			System.out.print("Database connected");

			PreparedStatement showRestaurantNameStatement = connection
					.prepareStatement("SELECT name, address, about, tel, type FROM restaurant WHERE restaurant.owner_username = ?");
			String ownerName = OwnerLogin.userName;
			
			showRestaurantNameStatement.setString(1, ownerName);
			
			ResultSet resultSet = showRestaurantNameStatement.executeQuery();
			
			if(resultSet.next()){
				jtfRestaurantName.setText(resultSet.getString(1));
				jtfRestaurantAddress.setText(resultSet.getString(2));
				jtfRestaurantDescription.setText(resultSet.getString(3));
				jtfRestaurantPhone.setText(resultSet.getString(4));
				jtfRestaurantType.setText(resultSet.getString(5));
				
			}
			else{
				jtfRestaurantName.setText("nothing found");
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public OwnerEdit() {

		JPanel ownerEditPanel = new JPanel();
		ownerEditPanel
				.setLayout(new BoxLayout(ownerEditPanel, BoxLayout.Y_AXIS));

		ownerEditPanel.add(Box.createVerticalStrut(5));
		ownerEditPanel.add(createJPanel1());
		ownerEditPanel.add(Box.createVerticalStrut(5));
		ownerEditPanel.add(createJPanel2());
		ownerEditPanel.add(Box.createVerticalStrut(5));
		ownerEditPanel.add(createJPanel3());
		ownerEditPanel.add(Box.createVerticalStrut(5));
		ownerEditPanel.add(createJPanel4());
		ownerEditPanel.add(Box.createVerticalStrut(30));
		ownerEditPanel.add(createJPanel5());
		ownerEditPanel.add(Box.createVerticalStrut(5));
		ownerEditPanel.add(createJPanel6());
		ownerEditPanel.add(Box.createVerticalStrut(5));
		ownerEditPanel.add(createJPanel7());
		ownerEditPanel.add(Box.createVerticalStrut(5));
		ownerEditPanel.add(createJPanel8());
		ownerEditPanel.add(Box.createVerticalStrut(5));

		jtfRestaurantName.setMaximumSize(jtfRestaurantName.getPreferredSize());
		jtfRestaurantPhone
				.setMaximumSize(jtfRestaurantPhone.getPreferredSize());
		jtfRestaurantType.setMaximumSize(jtfRestaurantType.getPreferredSize());
		jtfRestaurantAddress.setMaximumSize(jtfRestaurantAddress
				.getPreferredSize());
		jtfRestaurantDescription.setMaximumSize(jtfRestaurantDescription
				.getPreferredSize());
		jtfDishName.setMaximumSize(jtfDishName.getPreferredSize());
		jtfDishDescription
				.setMaximumSize(jtfDishDescription.getPreferredSize());
		jtfDishMainIngredient.setMaximumSize(jtfDishMainIngredient
				.getPreferredSize());
		
		

		jbtAddDish.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.out.print("s");
			}
		});

		this.setViewportView(ownerEditPanel);
	}

	private JPanel createJPanel1() {
		JPanel panel1 = new JPanel();
		panel1.setAlignmentX(LEFT_ALIGNMENT);
		panel1.setLayout(new BoxLayout(panel1, BoxLayout.X_AXIS));
		panel1.add(new JLabel("RESTAURANT INFO"));
		panel1.setBorder(BorderFactory.createLineBorder(Color.black));
		return panel1;
	}

	private JPanel createJPanel2() {
		JPanel panel2 = new JPanel();
		panel2.setAlignmentX(LEFT_ALIGNMENT);
		panel2.setLayout(new BoxLayout(panel2, BoxLayout.X_AXIS));
		panel2.add(new JLabel("Name: "));
		panel2.add(jtfRestaurantName);
		return panel2;
	}

	private JPanel createJPanel3() {
		JPanel panel3 = new JPanel();
		panel3.setAlignmentX(LEFT_ALIGNMENT);
		panel3.setLayout(new BoxLayout(panel3, BoxLayout.X_AXIS));
		panel3.add(new JLabel("Phone: "));
		panel3.add(jtfRestaurantPhone);
		panel3.add(new JLabel("Type: "));
		panel3.add(jtfRestaurantType);
		return panel3;
	}

	private JPanel createJPanel4() {
		JPanel panel4 = new JPanel();
		panel4.setAlignmentX(LEFT_ALIGNMENT);
		panel4.setLayout(new BoxLayout(panel4, BoxLayout.X_AXIS));
		panel4.add(new JLabel("Address: "));
		panel4.add(jtfRestaurantAddress);
		panel4.add(new JLabel("Description: "));
		panel4.add(jtfRestaurantDescription);
		panel4.add(jbtSave);
		return panel4;
	}

	private JPanel createJPanel5() {
		JPanel panel5 = new JPanel();
		panel5.setAlignmentX(LEFT_ALIGNMENT);
		panel5.setLayout(new BoxLayout(panel5, BoxLayout.X_AXIS));
		panel5.add(new JLabel("FOOD"));
		panel5.setBorder(BorderFactory.createLineBorder(Color.black));
		return panel5;
	}

	private JPanel createJPanel6() {
		JPanel panel6 = new JPanel();
		panel6.setAlignmentX(LEFT_ALIGNMENT);
		panel6.setLayout(new BoxLayout(panel6, BoxLayout.X_AXIS));
		panel6.add(new JLabel("Name: "));
		panel6.add(jtfDishName);
		return panel6;
	}

	private JPanel createJPanel7() {
		JPanel panel7 = new JPanel();
		panel7.setAlignmentX(LEFT_ALIGNMENT);
		panel7.setLayout(new BoxLayout(panel7, BoxLayout.X_AXIS));
		panel7.add(new JLabel("Description: "));
		panel7.add(jtfDishDescription);
		return panel7;
	}

	private JPanel createJPanel8() {
		JPanel panel8 = new JPanel();
		panel8.setAlignmentX(LEFT_ALIGNMENT);
		panel8.setLayout(new BoxLayout(panel8, BoxLayout.X_AXIS));
		panel8.add(new JLabel("Main Ingredient: "));
		panel8.add(jtfDishMainIngredient);
		panel8.add(jbtAddDish);

		return panel8;
	}
	
	public static void main(String[] args){
		OwnerEdit mainframe = new OwnerEdit();
		JFrame frame = new JFrame();
		frame.getContentPane().add(mainframe);
		frame.setSize(new Dimension(1100,650));
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setIconImage((new ImageIcon("image/restaurant-finder.png")).getImage());
		frame.setVisible(true);
	}
}